package programmercarl.贪心.C12;

import java.util.Arrays;

class Solution {
    public int candy(int[] ratings) {
        //从左到右，如果右子大于左子，则标志+1
        int[] candyNum = new int[ratings.length];
        candyNum[0] = 1;
        for (int i = 0; i < ratings.length-1; i++) {
            if (ratings[i] < ratings[i + 1]) {
                candyNum[i + 1] = candyNum[i] + 1;
            }else {
                candyNum[i+1]=1;
            }
        }
        //从右往左
        for (int i = ratings.length - 1; i > 0; i--) {
            if (ratings[i] < ratings[i-1]) {
                candyNum[i-1]=Math.max(candyNum[i]+1,candyNum[i-1]);//这里加1和i-1的比，不一定要+1
            }
        }
        return Arrays.stream(candyNum).sum();
    }

    //1 3 1 2 1
    public static void main(String[] args) {
        new Solution().candy(new int[]{1,3,2,2,1});
    }
}